package com.tzg157.fitness.kafka;

import cn.hutool.json.JSONObject;
import com.tzg157.fitness.service.realtime.HiveOnSparkService;
import com.tzg157.fitness.service.realtime.ads.ADSCourseTopNServiceImpl;
import com.tzg157.fitness.service.realtime.ods.ODSCoursePVServiceImpl;
import org.apache.spark.sql.SparkSession;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class MsgResolver implements Serializable {

    @Resource(name ="odsMemberInfoService")
    private HiveOnSparkService odsMemberService;
    @Resource(name ="dimMemberInfoService")
    private HiveOnSparkService dimMemberInfoService;
    @Resource(name ="dwsGenderService")
    private HiveOnSparkService dwsGenderService;
    @Resource
    private ODSCoursePVServiceImpl odsCoursePVService;
    @Resource(name ="odsCourseReserveService")
    private HiveOnSparkService odsCourseReserveService;
    @Resource(name ="odsCourseRateService")
    private HiveOnSparkService odsCourseRateService;

    @Resource
    private ADSCourseTopNServiceImpl courseTopNService;

    public void resolve(List<JSONObject> jsonObjects, SparkSession spark) {
        if(jsonObjects.isEmpty()){
            return;
        }

        //set hive.exec.mode.local.auto=true;
        //set hive.metastore.disallow.incompatible.col.type.changes=false;
        String type = (String) jsonObjects.get(0).get("type");
        if("register".equals(type)){
            for(JSONObject jsonObject : jsonObjects) {
                odsMemberService.save(jsonObject, spark);
                dimMemberInfoService.save(jsonObject, spark);
                dwsGenderService.save(jsonObject, spark);
            }
        }else if("coursePV".equals(type) || "courseReserve".equals(type) || "courseRate".equals(type)){
            if("coursePV".equals(type)){
                for(JSONObject jsonObject : jsonObjects) {
                    //odsCoursePVService.save(jsonObject, spark);
                }
            }else if("courseReserve".equals(type)){
                for(JSONObject jsonObject : jsonObjects) {
                    //odsCourseReserveService.save(jsonObject, spark);
                }
            }else{
                for(JSONObject jsonObject : jsonObjects) {
                    //odsCourseRateService.save(jsonObject, spark);
                }
            }
            courseTopNService.topn(spark);
        }
    }
}
